<?php

class Site extends Controller {
  function __construct() {
    parent::Controller();
    $this->is_logged_in();
  }

  function insertClub() {
    $this->load->model('crudrecord','crud',TRUE);
    $this->load->helper('schema');
    $schemas = loadSchemasFrom('schema/club/');

    $tClubFile =  downloadOwnClubWith($this->session->userdata['username'],$this->session->userdata['password']);
    $tClubDoc = loadXMLFromFile($tClubFile);
    $tClubId = $this->session->userdata['clubId'];

    foreach ($schemas as $schema) {
      $tData = array();
      $tableName = getTableNameFrom($schema);
      $tData[COL_CLUBID] = $tClubId;

      $tArray = parseXMLDoc($schema,$tClubDoc);
      if ($tArray == FALSE) {
        continue;
      }
      $tData = array_merge($tData, $tArray);
      $tArray = getFilterFrom($schema);
      $tData = array_merge($tData, $tArray);
      $this->crud->create($tableName,$tData);
    }
    unlink($tClubFile);
  }

  function insertPlayers() {
    $this->load->model('crudrecord','crud',TRUE);
    $this->load->helper('schema');
    $schemas = loadSchemasFrom('schema/player/');

    $tPlayerFile =  downloadOwnPlayersWith($this->session->userdata['username'],$this->session->userdata['password']);
    $tPlayerDoc = loadXMLFromFile($tPlayerFile);

    $xPath = new DOMXPath($tPlayerDoc);
    $qResult = $xPath->query('//player');

    foreach ($qResult as $result) {
      $tDoc = DOMDocument::loadXML($tPlayerDoc->saveXML($result));
      $tXPATH = new DOMXPath($tDoc);
      $tResult = $tXPATH->query('./@id');
      $tPlayerId = $tResult->item(0)->nodeValue;

      foreach ($schemas as $schema) {
        $tData = array();
        $tableName = getTableNameFrom($schema);
        $tData[COL_PLAYERID] = $tPlayerId;

        $tArray = parseXMLDoc($schema,$tDoc);
        if ($tArray == FALSE) {
          continue;
        }
        $tData = array_merge($tData, $tArray);
        $tArray = getFilterFrom($schema);
        $tData = array_merge($tData, $tArray);
        $this->crud->create($tableName,$tData);
      }
    }
    unlink($tPlayerFile);
  }

  function first_login() {
    $this->insertClub();
    $this->insertPlayers();

    $data['header_title'] = 'pFMPortal User Area';
    $data['main_content'] = 'site/index';
    $data['style'] = 'default';

    $this->load->view('includes/template', $data);
  }

  function updateClub() {
    $this->load->model('crudrecord','crud',TRUE);
    $this->load->helper('schema');
    $schemas = loadSchemasFrom('schema/club/');

    $tClubFile =  downloadOwnClubWith($this->session->userdata['username'],$this->session->userdata['password']);
    $tClubDoc = loadXMLFromFile($tClubFile);
    $tClubId = $this->session->userdata['clubId'];

    foreach ($schemas as $schema) {
      $tData = array();
      $tableName = getTableNameFrom($schema);
      $where = getFilterFrom($schema);
      $where[COL_CLUBID] = $tClubId;

      $tData = parseXMLDoc($schema,$tClubDoc);
      if ($tData == FALSE) {
        continue;
      }
      $tId = $this->crud->getId($tableName,$where);
      $this->crud->update($tableName,$tId[COL_ID],$tData);
    }
  }

  function updatePlayers() {
    $this->load->model('crudrecord','crud',TRUE);
    $this->load->helper('schema');
    $schemas = loadSchemasFrom('schema/player/');

    $tPlayerFile =  downloadOwnPlayersWith($this->session->userdata['username'],$this->session->userdata['password']);
    $tPlayerDoc = loadXMLFromFile($tPlayerFile);

    $xPath = new DOMXPath($tPlayerDoc);
    $qResult = $xPath->query('//player');

    foreach ($qResult as $result) {
      $tDoc = DOMDocument::loadXML($tPlayerDoc->saveXML($result));
      $tXPATH = new DOMXPath($tDoc);
      $tResult = $tXPATH->query('./@id');
      $tPlayerId = $tResult->item(0)->nodeValue;

      foreach ($schemas as $schema) {
        $tData = array();
        $tableName = getTableNameFrom($schema);
        $where = getFilterFrom($schema);
        $where[COL_PLAYERID] = $tPlayerId;

        $tData = parseXMLDoc($schema,$tDoc);
        if ($tData == FALSE) {
          continue;
        }
        $tId = $this->crud->getId($tableName,$where);
        $this->crud->update($tableName,$tId[COL_ID],$tData);
      }
    }
  }

  function members_area() {
    $this->load->model('crudrecord','crud',TRUE);

    if ($this->session->userdata['lastLogin'] == NULL) {
      redirect('site/first_login');
    }

    $this->updateClub();
    //    $this->updatePlayers();
    //
    //    $tPlayerFile = $this->freekick->downloadOwnPlayersWith($this->session->userdata['username'],$this->session->userdata['password']);
    //    $tPlayerDoc = $this->freekick->loadXMLFromFile($tPlayerFile);
    //
    //    $xPath = new DOMXPath($tPlayerDoc);
    //    $qResult = $xPath->query('//player');
    //    foreach ($qResult as $result) {
    //      $tDoc = DOMDocument::loadXML($tPlayerDoc->saveXML($result));
    //      $tXPATH = new DOMXPath($tDoc);
    //      $tResult = $tXPATH->query('./@id');
    //      $tPlayerId = $tResult->item(0)->nodeValue;
    //    }

    $data['header_title'] = 'pFMPortal User Area';
    $data['main_content'] = 'site/index';
    $data['style'] = 'default';

    $this->load->view('includes/template', $data);
  }

  function is_logged_in() {
    $is_logged_in = $this->session->userdata('is_logged_in');
    if(!isset($is_logged_in) || $is_logged_in != TRUE) {
      echo 'You don\'t have permission to access this page. <a href="../login">Login</a>';
      log_message('info','Unauthorized access from: '.$this->input->ip_address());
      redirect('login');
    }
  }

}
